/*/ ----------------------------------------------------------------------------
 * CheckTablespace.java 20101206 ndarkduck version 1.0
 * e-mail ndarkduck[ta]gma1l.com, this is a code released under the GPL 3.0
 * url: http://code.google.com/p/checkibm/
 * 
 *
 * Pre-requisites: DB2 JDBC driver installed and DB user has these minimum rights:
 *
 *   SYSMON authority
 *   SELECT or CONTROL privilege on the SNAPHADR administrative view and EXECUTE privilege on the SNAP_GET_HADR table function.
 *   
 *   
 * */
package com.google.code.checkibm.db2;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import com.google.code.checkibm.UsageException;

public class CheckDb2HADR implements com.google.code.checkibm.CheckPlugin
{
    int return_code         = 3;        // the return code for the plugin
    String message_data     = "";       // the message the user should see on nagios console
    String performance_data = "";       // the variables that should be analized on nagios
    
    String database         = "";       // db to connect
    String role             = "";
    String state            = "";
    String syncmode         = "";
    String connstatus       = "";
    
    String plugin_usage_msg = "db2_hadr: -H host -l username -a password\n-F               database=databasename" +
                                "[role={STANDARD|PRIMARY|SECONDARY}]"+
                                "[,state={DISCONNECTED|LOCAL_CATCHUP|PEER|REM_CATCH_PEN|REM_CATCHUP}]"+
                                "[,syncmode={ASYNC|NEARSYNC|SYNC}][connstatus={CONGESTED|CONNECTED|DISCONNECTED}]";       

    
    public CheckDb2HADR(String host, String port, String strargs, String dbUser, String dbPass, float crit_at, float warn_at) throws Exception
    {
        // local parse
        String arguments[] = strargs.split(",");
        String argument[];
        for (int i = 0; i < arguments.length; i++)
        {
            argument = arguments[i].split("=");
            if (argument.length < 2)
                throw new UsageException(plugin_usage_msg);
            if (argument[0].trim().equals("database"))
                database = argument[1].trim();
            else if (argument[0].trim().equals("role"))
                role = argument[1].trim().toUpperCase();
            else if (argument[0].trim().equals("state"))
                state = argument[1].trim().toUpperCase();
            else if (argument[0].trim().equals("syncmode"))
                syncmode = argument[1].trim().toUpperCase();
            else if (argument[0].trim().equals("connstatus"))
                connstatus = argument[1].trim().toUpperCase();

        }

        return_code = 0;
        String query = "";

        // Minimal checks
        Class.forName("com.ibm.db2.jcc.DB2Driver"); // throws
                                                    // ClassNotFoundException
        if (dbUser == "" || dbPass == "" || database == "")
            throw new UsageException(plugin_usage_msg);

        String dbUrl = "jdbc:db2://" + host + ":" + port + "/" + database;
        Connection connection = DriverManager.getConnection(dbUrl, dbUser, dbPass);

        query = "SELECT DB_NAME, HADR_ROLE, HADR_STATE, HADR_SYNCMODE, HADR_CONNECT_STATUS FROM SYSIBMADM.SNAPHADR";

        Statement statement = connection.createStatement();
        ResultSet rs = statement.executeQuery(query);
        boolean rsempty = true;
        while (rs.next())
        {
            rsempty = false;
            String rsdbname = rs.getString(1);
            String rsrole = rs.getString(2);
            String rsstate = rs.getString(3);
            String rssyncmode = rs.getString(4);
            String rsconnstatus = rs.getString(5);
            message_data += rsdbname+" HADR_ROLE="+rsrole+", HADR_STATE="+rsstate+", HADR_SYNCMODE="+rssyncmode+
            ", HADR_CONNECT_STATUS="+rsconnstatus;
            
            if (role != "" && !rsrole.toUpperCase().equals(role))            
                return_code = 2;             
            if (state != "" && !rsstate.toUpperCase().equals(state))            
                return_code = 2;
            if (syncmode != "" && !rssyncmode.toUpperCase().equals(syncmode))            
                return_code = 2;             
            if (connstatus != "" && !rsconnstatus.toUpperCase().equals(connstatus))            
                return_code = 2;
        }
        if(return_code == 0)
            performance_data=database+"_status=0;1;2;0;3";
        else if(return_code == 2)
            performance_data=database+"_status=2;1;2;0;3";
        if(rsempty)
        {
            return_code = 3;
            message_data = database+" returned no HADR data";
            performance_data=database+"_status=3;1;2;0;3";
        }
        rs.close();
        statement.close();
        connection.close();

    }
    
    public int checkStatus()
    {
        return return_code;
    }
    
    public String get_message()
    {
        return message_data;
    }
    
    public String get_performance_data()
    {      
        return performance_data;
    }
}
